import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame } from 'cc';


import { IEntity } from '../Levels';
import { PlayerStateMachine } from '../Scripts/Player/PlayerStateMachine';
import { TILE_WIDTH, TILE_HEIGHT } from '../Scripts/Tile/TileManager';
import { DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, PARAMS_NAME_ENUM } from '../Enums';
import { StateMachine } from './StateMachine';
import { randomByLen } from '../Utils';
const { ccclass, property } = _decorator;



@ccclass('EntityManager')
export class EntityManager extends Component {
    id:string = randomByLen(12)
    x: number = 0;
    y: number = 0;

    fsm: StateMachine;

    private _direction: DIRECTION_ENUM;
    private _state: ENTITY_STATE_ENUM;
    private type: ENTITY_TYPE_ENUM

    public get direction(): DIRECTION_ENUM {
        return this._direction;
    }
    public set direction(newDirection: DIRECTION_ENUM) {
        this._direction = newDirection;
        this.fsm.setParams(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction]); // 设置状态机的参数
    }
    public get state(): ENTITY_STATE_ENUM {
        return this._state;
    }
    public set state(newState: ENTITY_STATE_ENUM) {
        this._state = newState;
        this.fsm.setParams(this._state, true); // 设置状态机的参数
    }


    async init(params:IEntity) {
        
        const sprite = this.addComponent(Sprite);
        sprite.sizeMode = Sprite.SizeMode.CUSTOM;
        const transform = this.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4);


        
        this.x = params.x;
        this.y = params.y;
        this.type = params.type;
        this.direction = params.direction;
        this.state = params.state;

    }

    protected update(dt: number): void {
        this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT + TILE_HEIGHT * 1.5)
    }

    protected onDestroy(): void {
        
    }



}


